$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'lanling/student/list',
        datatype: "json",
        colModel: [
            {label: 'userId', name: 'userId', index: 'user_id', width: 50, key: true, hidden: true},
            {label: '用户名', name: 'username', index: 'username', width: 80, align: "center"},
            {label: '真实姓名', name: 'realname', index: 'realname', width: 80, align: "center"},
            {label: '身份证', name: 'idCard', index: 'id_card', width: 80, align: "center"},
            {
                label: '性别', name: 'sex', index: 'sex', width: 80, align: "center",
                formatter: function (value) {
                    if (value == 1) {
                        return '<span class="label label-success">男</span>';
                    } else if (value == 2) {
                        return '<span class="label label-warning">女</span>';
                    } else {
                        return '<span class="label label-danger">保密</span>';
                    }
                }
            },
            {label: '手机号', name: 'mobile', index: 'mobile', width: 80, align: "center"},
            {label: '登录密码', name: 'passwordRecord', index: 'password_record', width: 80, align: "center"},
            {label: '登陆次数', name: 'loginCount', index: 'login_count', width: 80, align: "center"},
            {
                label: '状态',
                name: 'state',
                index: 'state',
                width: 80,
                align: "center",
                formatter: function (value) {
                    if (value == 1) {
                        return '<span class="label label-success">启用</span>';
                    } else if (value == 2) {
                        return '<span class="label label-warning">禁用</span>';
                    } else if (value == 3) {
                        return '<span class="label label-danger">删除</span>';
                    } else {
                        return '<span class="label label-danger">未知</span>';
                    }
                }
            },
            {label: '创建时间', name: 'createTime', index: 'create_time', width: 80, align: "center"}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        student: {},
        ruleValidate: {
            realname: [
                {required: true, message: '学员姓名不能为空', trigger: 'blur'},
                {max: 100, message: '请输入最多100位'}
            ],
            mobile: [
                {required: true, message: '手机号不能为空', trigger: 'blur'}
            ],
            idCard: [
                {required: true, message: '身份证不能为空', trigger: 'blur'}
            ],
            passwordRecord: [
                {required: true, message: '登录密码不能为空', trigger: 'blur'}
            ],
            sex: [
                {required: true, message: '性别不能为空', trigger: 'blur'}
            ],
            state: [
                {required: true, message: '状态不能为空', trigger: 'blur'}
            ]

        },
        q: {
            name: ''
        },
        subjectList: []
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.student = {
                state: '1',
                sex: '1',
                passwordRecord: '123456'
            };
            vm.subjectList = [];
            vm.getSignUpSubjectList();
        },
        update: function (event) {
            var userId = getSelectedRow();
            if (userId == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getSignUpSubjectList();
            vm.getInfo(userId)
        },
        saveOrUpdate: function (event) {
            var url = vm.student.userId == null ? "lanling/student/save" : "lanling/student/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(vm.student),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var userIds = getSelectedRows();
            if (userIds == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "lanling/student/delete",
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(userIds),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (userId) {
            $.get(baseURL + "lanling/student/info/" + userId, function (r) {
                vm.student = r.student;
                vm.student.sex = vm.student.sex + '';
                vm.student.state = vm.student.state + '';
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name.trim()},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getSignUpSubjectList: function () {
            $.get("../../lanling/subject/queryList", function (r) {
                vm.subjectList = r.list;
            });
        },
        searchDetail: function () {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            openWindow({
                title: '搜索记录',
                type: 2,
                content: '../lanling/search.html?studentId=' + id
            })
        }
    }
});